package com.app;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

public class PopupListener extends MouseAdapter {

	JPopupMenu popup = new JPopupMenu();
	static JTable myTable;

	void init() {
		JMenuItem menuItem;
		menuItem = new JMenuItem("IPO");
		menuItem.addActionListener(new PopupActionListener());
		popup.add(menuItem);
		menuItem = new JMenuItem("BONUS");
		menuItem.addActionListener(new PopupActionListener());
		popup.add(menuItem);
		menuItem = new JMenuItem("SPLIT");
		menuItem.addActionListener(new PopupActionListener());
		popup.add(menuItem);
	}

	PopupListener(JTable mytable) {
		myTable = mytable;
		init();
	}

	public void mousePressed(MouseEvent e) {
		maybeShowPopup(e);
	}

	public void mouseReleased(MouseEvent e) {
		maybeShowPopup(e);
	}

	private void maybeShowPopup(MouseEvent e) {
		if (e.isPopupTrigger()) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	class PopupActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println(e.getActionCommand());
			processAction(e.getActionCommand());
		}

		void processAction(String action) {
			InsertTrade it = new InsertTrade();
			it.getAction().setText(action);
			it.getStock().setText((String)PopupListener.myTable.getModel().getValueAt(PopupListener.myTable.getSelectedRow(), 2));
			it.pack();
			it.setVisible(true);
		}

	}
}
